package com.tjetc.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@SuppressWarnings("all")
public class _2GetReflectInformation {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        /*
            演示可以获取一些类的信息
         */

        // 获取类的信息 由于类只会加载一次 class对象只会有一份
        Class<?> aClass = Class.forName("com.tjetc.reflect._1Cat");
        System.out.println("aClass.hashCode() = " + aClass.hashCode());
        Class<?> bClass = Class.forName("com.tjetc.reflect._1Cat");
        System.out.println("bClass.hashCode() = " + bClass.hashCode());

        // 获取构造方法
        Constructor constructor = aClass.getDeclaredConstructor(String.class, Integer.class);

        Object o = constructor.newInstance("🐱", 1);
        System.out.println(o);
        // 目前只能拿到public属性
        Field name = aClass.getDeclaredField("name"); // 在反射面前没有private
        name.setAccessible(true);
        System.out.println(name);

        Method method = aClass.getMethod("hi");
        // 方法的执行
        method.invoke(o);




    }
}
